<template>
  <view class="pdf-container">
    <web-view :src="pdfViewerUrl" class="pdf-viewer"></web-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      url: "",
    };
  },
  onLoad(e) {
    if (e.url) {
      this.url = e.url;
      // 检查URL是否需要格式化
      if (!this.url.startsWith('http')) {
        this.url = 'https://ticket.sxd.cn' + (this.url.startsWith('/') ? '' : '/') + this.url;
      }
    } else {
      uni.showToast({
        title: '无效的PDF文件路径',
        icon: 'none'
      });
      setTimeout(() => {
        uni.navigateBack();
      }, 1500);
    }
  },
  computed: {
    pdfViewerUrl() {
      if (!this.url) return '';
      
      // 使用PDF.js查看器加载PDF
      return `https://mozilla.github.io/pdf.js/web/viewer.html?file=${encodeURIComponent(this.url)}`;
    }
  },
  methods: {
    // 如果需要返回上一页
    goBack() {
      uni.navigateBack();
    }
  }
};
</script>

<style lang="scss">
@import '../../uni.scss';

.pdf-container {
  width: 100%;
  height: 100vh;
  position: relative;
  
  .pdf-viewer {
    width: 100%;
    height: 100%;
  }
}
</style>
